-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix sorting bugs (esp MissingOptimization
) that come up when using SortingAlgorithms.TimSort
#50171
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LilithHafner
added
bugfix
This change fixes an existing bug
missing data
Base.missing and related functionality
sorting
Put things in order
backport 1.9
Change should be backported to release-1.9
labels
Jun 14, 2023
LilithHafner
changed the title
Fix some MissingOptimization bugs that come up when using SortingAlgorithms.TimSort
Fix sorting bugs (esp Jun 15, 2023
MissingOptimization
) that come up when using SortingAlgorithms.TimSort
@nanosoldier |
Your package evaluation job has completed - no new issues were detected. |
@nanosoldier |
Your package evaluation job has completed - no new issues were detected. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bugfix
This change fixes an existing bug
missing data
Base.missing and related functionality
sorting
Put things in order
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
These bugs in Base led to a bug in DataFrames.jl. The bugs this fixes are regressions introduced in 1.9.
The systemic issue is that Base only uses and therefore only tests a subset of its sorting internals, while SortingAlgorithms.jl uses a larger subset, and SortingAlgorithms.jl has poor testing.
More specifically, SortingAlgorithms.TimSort is defined as
Base.Sort.InitialOptimizations(TimSortAlg())
, which results in some preprocessing taking place before theBase.sort!(v::AbstractVector, ::TimSortAlg, ::Ordering)
method gets called. This preprocessing results inv
sometimes being aBase.Sort.WithoutMissingVector
. TimSort callsv[b]
whereb
is a unit range (why? idk, it would probably be more performant not to but I haven't read the whole algorithm). This is a perfectly reasonable thing to do, butgetindex(::Base.Sort.WithoutMissingOptimization, ::UnitRange)
was buggy. This PR fixes that.Also, there was a bug in the fastpath for filtering
missing
s to the end under aPerm
ordering. We (I) assumed thatlo:hi == eachindex(v)
, which is not guaranteed. It's pretty hard to trigger this bug without calling a 5-argsort!
method, but TimSort does call such a method as its base case, so this branch is broken there too. Again, this PR removes the buggy assumption thatlo:hi == eachindex(v)
.Finally, the mechanism to implicitly convert the old 3- 5- or 6-argument
sort!
calling convention to the new 4-argument_sort!
and back without triggering infinite loops when neither is defined was too strict about infinite loop detection. It prohibits anything that calls into sorting using the old convention, implicitly converts to the new convention, and then at any point later implicitly converts back to the old convention. This prohibits, for example, callingsort!(v, SortingAlgorithms.TimSort, Base.Order.Forward)
becauseSortingAlgorithms.TimSort
is defined asBase.Sort.InitialOptimizations(SortingAlgorithms.TimSortAlg())
, so that old-stylesort!
call is implicitly converted to the new style, benefits from modern intermediary layers, and then implicitly converted back to the old style for TimSortAlg. This throws because of the conversion back and forth. Technically, this bug isn't visible in the public API becauseInitialOptimizations
is internal, as is_sort!
but it feels bad enough to be worth backporting a fix. The fix is to switch from tracking "has there been a legacy dispatch into this sorting call chain" to "what was the alg that was used at that entrypoint, if any". Then, we only throw if implicitly convert to the_sort!
system and then implicitly convert back out without unwrapping a single layer of the algorithm, which is a much tighter bound than we had before.Also makes a misleading error message not misleading, which I'd also call a bugfix in this case.